
using Yuebon.Commons.Pages;

namespace Yuebon.InvoiceApi.Areas.Security.Controllers
{
    /// <summary>
    /// 发票信息接口
    /// </summary>
    [ApiController]
    [Route("api/Security/[controller]")]
    public partial class InvoiceController : AreaApiController<THInvoice, THInvoiceOutputDto,THInvoiceInputDto,ITHInvoiceService>
    {
        private readonly IWebHostEnvironment _env;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_iService"></param>
        public InvoiceController(ITHInvoiceService _iService, IWebHostEnvironment env) : base(_iService)
        {
            iService = _iService;
            _env = env;
        }


        /// <summary>
        /// 异步分页查询
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost("SearchPageList")]
        [YuebonAuthorize("List")]
        public async Task<IActionResult> SearchPageList(InvoiceQueryModel search)
        {
            CommonResult<PageResult<THInvoiceOutputDto>> result = new CommonResult<PageResult<THInvoiceOutputDto>>();
            result.ResData = await iService.SearchPageList(search);
            result.ErrCode = ErrCode.successCode;
            return ToJsonContent(result);
        }


        /// <summary>
        /// 查询发票信息
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost("InvoiceList")]
        [YuebonAuthorize("List")]
        public async Task<IActionResult> SearchInvoiceList(InvoiceQueryModel search)
        {
            CommonResult Result = new CommonResult();
            List<string> rsr = new List<string>();
            if (!string.IsNullOrEmpty(search.Keywords))
            {
                var invoices = iService.SearchInvoiceList(search.Keywords);
                invoices.Result.ForEach(item =>
                {
                    string rs = $"01,32,,{item.InvoiceNo},{item.Money},{Convert.ToDateTime(item.InvoiceDate).ToString("yyyyMMdd")},,F422";
                    rsr.Add(rs);
                });
            }

            Result.ResData = rsr;
            Result.Success = true;
            Result.ErrMsg = "";
            Result.ErrCode = "0";
           
            return ToJsonContent(Result);
        }

        /// <summary>
        /// 扫码核验
        /// </summary>
        /// <param name="invoice"></param>
        /// <returns></returns>
        [HttpPost("ScanInvoice")]        
        public async Task<IActionResult> ScanInvoice(InvoiceQueryModel search)
        {
            CommonResult<InvoiceScanOutDto> result = new CommonResult<InvoiceScanOutDto>();
            result.ResData = await iService.ScanInvoiceNo(search.Keywords);
            result.ErrCode = ErrCode.successCode;
            return ToJsonContent(result);
        }

        /// <summary>
        /// 扫码核验
        /// </summary>
        /// <param name="invoice"></param>
        /// <returns></returns>
        [HttpPost("ImportInvoices")]
        public async Task<IActionResult> ImportInvoices(InvoiceQueryModel search)
        {
            CommonResult<InvoiceScanOutDto> result = new CommonResult<InvoiceScanOutDto>();
            string uploadFilePath = Path.Combine(_env.WebRootPath, search.Keywords);

            result.ResData = await iService.ImportInvoices(uploadFilePath);
            result.ErrCode = ErrCode.successCode;            
            return ToJsonContent(result);
        }

        /// <summary>
        /// 扫码核验
        /// </summary>
        /// <param name="invoice"></param>
        /// <returns></returns>
        [HttpPost("ExportInvoices")]
        public async Task<IActionResult> ExportInvoices(InvoiceQueryModel search)
        {
            CommonResult<InvoiceScanOutDto> result = new CommonResult<InvoiceScanOutDto>();
           

            result.ResData = await iService.ExportInvoice(search, _env.WebRootPath);
            result.ErrCode = ErrCode.successCode;
            return ToJsonContent(result);
        }

        /// <summary>
        /// 去重
        /// </summary>
        /// <param name="invoices"></param>
        /// <returns></returns>
        [HttpPost("FilterInvoices")]
        public async Task<IActionResult> FilterInvoices(List<InvoiceScanOutDto> invoices)
        {
            CommonResult<InvoiceScanOutDto> result = new CommonResult<InvoiceScanOutDto>();
            result.ResData = await iService.FilterInvoice(invoices);
            result.ErrCode = ErrCode.successCode;
            return ToJsonContent(result);
        }


        /// <summary>
        /// 批量核验
        /// </summary>
        /// <param name="invoices"></param>
        /// <returns></returns>
        [HttpPost("BatchInvoices")]
        public async Task<IActionResult> BatchInvoices(List<InvoiceScanOutDto> invoices)
        {
            CommonResult<InvoiceScanOutDto> result = new CommonResult<InvoiceScanOutDto>();
            result.ResData = await iService.BatchInvoice(invoices);
            result.ErrCode = ErrCode.successCode;
            return ToJsonContent(result);
        }
    }
}